<!--# include virtual="/admin/common/html/header.html"-->
<nav class="breadcrumb">
	<i class="Hui-iconfont">&#xe67f;</i> 首页 
	<span class="c-gray en">&gt;</span>数据共享管理 
	<span class="c-gray en">&gt;</span>数据共享分部分项
	    <a class="btn btn-success radius r" 
	    	style="line-height:1.6em;margin-top:3px" 
	    	href="javascript:location.replace(location.href);" 
	    	title="刷新" >
	    	<i class="Hui-iconfont">&#xe68f;</i>
	    </a>
</nav>
<body style="overflow: hidden;width: auto;">
<section class="Hui-article-box" style="width: 120%;z-index: 0;">
	<div style="margin-left: 10px;height: 100%;">
		<ul id="treeDemo" class="ztree" style="height: 100%;overflow-y: scroll;padding: 0;"></ul>
	</div>
</div>
</section>
<!--# include virtual="/admin/common/html/_footer.html"-->
<script type="text/javascript">
var setting = {
	view: {
		dblClickExpand: true,
		showLine: true,
		selectedMulti: true,
		addHoverDom: addHoverDom
	},
	data: {
		simpleData: {
			enable:true,
			idKey: "catalogId",
			pIdKey: "catalogParentId",
			rootPId: "0"
		},
		key:{
			name: "catalogName"
		}
	},
	edit: {
		enable: true,
		editNameSelectAll: true,
		showRemoveBtn: true,
		removeTitle: "点击删除节点",
		showRenameBtn: true,
		renameTitle: "点击编辑节点",
		drag: {
			isCopy: false,
			isMove: true
		}
	},
	callback: {
		beforeDrag: beforeDrag,
		beforeEditName: beforeEditName,
		beforeRemove: beforeRemove,
		beforeRename: beforeRename,
		onRemove: onRemove,
		onRename: onRename,
		beforeClick: function(treeId, treeNode) {
			if(isclick){
				return;
			}
			isclick = true;
			var guidanceId = GetQueryString("guidanceId");
			var infoId = GetQueryString("infoId");
			var newInfo = GetQueryString("newInfo");
			if(((!infoId)||(infoId=="null"))&&((!newInfo)||(newInfo!="1"))){
				return;
			}
			if(parent.upCatalog){
				parent.upCatalog(treeNode.catalogId,infoId);
			}
			var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
		    parent.layer.close(index);
		}
	}
};
var isclick;
$.post('/guidance_service/front/data/catalog/getCatalogs.do',{guidanceId:GetQueryString('guidanceId')},function(obj){
	var data=obj.data || new Array();
	if(data && data.length){
		$.fn.zTree.init($("#treeDemo"), setting, data);
	}else{
		data = data.concat({"catalogName":"目录","catalogId":0})
		$.fn.zTree.init($("#treeDemo"), setting, data);
	}
});
function updateObj(upobj){
	$.ajax({
		type : "post",
	    url : '/guidance_service/front/data/catalog/update.do', 
	    async : false, 
	    data : {
	    	catalogName:upobj.catalogName,
	    	catalogParentId:upobj.catalogParentId,
	    	catalogId:upobj.catalogId
	    },
		success:function(obj){
			if(obj.resultCode == 200){
				returnObj = obj.data;
			}else{
				
			}
		}
	});
	return returnObj;
}
function addNode(){
	$.ajax({
		type : "post", 
        url : '/guidance_service/front/data/get.do', 
        data : {guidanceId:guidanceId}, 
		success:function(obj){
		var data = obj.data;
		catalogName=data.guidanceName;
	}});
	var data ={
		"catalogId":randomParent,
		"catalogName":catalogName,
		"guidanceId":guidanceId,
		"catalogParentId":"0"
	}
	
	$.ajax({
		type : "post", 
        url : '/guidance_service/front/data/catalog/add.do', 
        data : data,
        async : false, 
		success:function(obj){
			if(obj.resultCode == 200){
				resultObj=obj.data;
			}else{
			}
		}
	});
	return resultObj;
}
function beforeDrag(treeId, treeNodes) {
	return false;
}
var log, className = "dark";
function beforeEditName(treeId, treeNode) {
	className = (className === "dark" ? "":"dark");
	showLog("[ "+getTime()+" beforeEditName ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.selectNode(treeNode);
	layer.prompt({
		title: '编辑节点',
		value:treeNode.catalogName
	}, function(text, index){
		var data ={
			"catalogName":text.trim(),
			"catalogId":treeNode.catalogId
		}
		$.post("/guidance_service/front/data/catalog/update.do",data,function(result){
			if(result.resultCode == 200){
				treeNode.catalogName=text.trim();
				$.fn.zTree.getZTreeObj("treeDemo").updateNode(treeNode);
			    layer.close(index);
			}else{
				layer.msg(result.message,{icon:2,time:1000},function(){
					layer.close(index);
				});
			}
		});
	});
	return false;
}
function beforeRemove(treeId, treeNode) {
	console.dir(treeNode);
	className = (className === "dark" ? "":"dark");
	showLog("[ "+getTime()+" beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.selectNode(treeNode);
	layer.confirm('确认删除-'+treeNode.catalogName+'-节点？', {
	  btn: ['确定','取消'] //按钮
	}, function(index){
		$.post("/guidance_service/front/data/catalog/logicDelete.do",{"catalogId":treeNode.catalogId},function(result){
			zTree.removeNode(treeNode);
	    	layer.close(index);
		});	
	}, function(index){
		 layer.close(index);
	});
	return false;
}
function onRemove(e, treeId, treeNode) {
	showLog("[ "+getTime()+" onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
}
function beforeRename(treeId, treeNode, newName, isCancel) {
	className = (className === "dark" ? "":"dark");
	showLog((isCancel ? "<span style='color:red'>":"") + "[ "+getTime()+" beforeRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name + (isCancel ? "</span>":""));
	if (newName.length == 0) {
		setTimeout(function() {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.cancelEditName();
			alert("节点名称不能为空.");
		}, 0);
		return false;
	}
	var  data = {
		catalogId:treeNode.catalogId,
		catalogName: newName,
	}
	$.post("/guidance_service/front/data/category/update.do",data,function(result){
			console.dir(result);
	});
	return true;
}
function onRename(e, treeId, treeNode, isCancel) {
	showLog((isCancel ? "<span style='color:red'>":"") + "[ "+getTime()+" onRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name + (isCancel ? "</span>":""));
}
function showRemoveBtn(treeId, treeNode) {
	return !treeNode.isFirstNode;
}
function showRenameBtn(treeId, treeNode) {
	return !treeNode.isLastNode;
}
function showLog(str) {
	if (!log) log = $("#log");
	log.append("<li class='"+className+"'>"+str+"</li>");
	if(log.children("li").length > 8) {
		log.get(0).removeChild(log.children("li")[0]);
	}
}

var newCount = 1;
function addHoverDom(treeId, treeNode) {
	if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
	var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
		+ "' title='新增节点' onfocus='this.blur();'></span>";
	$("#" + treeNode.tId + "_span").after(addStr);
	var btn = $("#addBtn_"+treeNode.tId);
	if (btn) btn.bind("click", function(){
		layer.prompt({title: '输入节点名称，并确认'}, function(text, index){
			var lastcatalogId = treeNode.children? treeNode.children[(treeNode.children.length-1)].catalogId:treeNode.catalogId*1000;
			var data ={
				"catalogName":text.trim(),
				"catalogParentId":treeNode.catalogId,
				"guidanceId":treeNode.guidanceId,
				"lastcatalogId":lastcatalogId
			}
			$.post("/guidance_service/front/data/catalog/add.do",data,function(result){
				if(result.resultCode == 200){
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				    zTree.addNodes(treeNode, {catalogId:result.data.catalogId, pId:result.data.catalogParentId, catalogName:result.data.catalogName});
				    layer.close(index);
				}else{
					layer.msg(result.message,{icon:2,time:1000},function(){
						layer.close(index);
					});
				}
			});
		});
		return false;
	});
};

function removeHoverDom(treeId, treeNode) {
	$("#addBtn_"+treeNode.tId).unbind().remove();
};
function selectAll() {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.setting.edit.editNameSelectAll =  $("#selectAll").attr("checked");
}
$(document).ready(function(){
	$("#selectAll").bind("click", selectAll);
});
</script>
</body>
</html>